﻿package com.hongguan.oms.service.grid;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.hongguan.oms.domain.grid.GridAuthInfo;
import com.hongguan.oms.domain.grid.GridInfo;
import com.hongguan.oms.service.BaseService;

/**
 * GridCommunityInfo Service
 * 
 * @author 
 * 
 */
@SuppressWarnings("unchecked")
@Service
public class GridCommunityInfoService extends BaseService {
	public GridCommunityInfoService() {
		super();
		namespace = "GRID_COMMUNITY_INFO";
	} 
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchCommunity(Map param){
		return this.getSqlMapClientTemplate().queryForList(this.getStatementName("community_search"), param);
	}
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchCommunityById(Map param){
		return this.getSqlMapClientTemplate().queryForList(this.getStatementName("community_search_by_id"), param);
	}
	public boolean isNameSame(Map param){
		boolean flag=false;
		long num=(Long)this.getSqlMapClientTemplate().queryForObject(this.getStatementName("is_community_name_same"),param);
		if(num>0){
			flag=true;
		}
		return flag;
	}
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchUnitManagerById(Map param){
		return this.getSqlMapClientTemplate().queryForList("UserInfo.user_search_by_id", param);
	}
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchUnitById(Map param){
		return this.getSqlMapClientTemplate().queryForList("GRID_UNIT.unit_search_by_id", param);
	}
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchContactById(Map param){
		return this.getSqlMapClientTemplate().queryForList("GRID_CONTACT_INFO.contact_search_by_id", param);
	}
	/**
	 * search grid community infomation
	 * @param param
	 * @return
	 */
	public List searchGroupCustById(Map param){
		return this.getSqlMapClientTemplate().queryForList("GRID_GROUP_CUST_INFO.group_cust_search_by_id", param);
	}
	/**
	 * search grid community count
	 * @param param
	 * @return
	 */
	public Long searchCommunitycount(Map param){
		return (Long)this.getSqlMapClientTemplate().queryForObject(this.getStatementName("community_search_count"), param);
	}
	/**
	 * insert one record into community
	 * @param param
	 */
	public long addCommunity(Map param){
		return (Long)this.getSqlMapClientTemplate().insert(this.getStatementName("community_add"), param);
	}
	/**
	 * delete one record from community
	 * @param param
	 */
	public void deleteCommunity(Map param){
		this.getSqlMapClientTemplate().delete(this.getStatementName("community_delete"),param);
		this.deleteGis(param);
		this.deleteUnit(param);
		this.deleteGroupCust(param);
		this.deleteContact(param);
		this.deleteNC(param);
		this.deleteAddress(param);
	}
	public void deleteGis(Map param){
		this.getSqlMapClientTemplate().delete("GIS_INFO.gis_delete", param);
	}
	public void deleteUnit(Map param){
		this.getSqlMapClientTemplate().delete("GRID_UNIT.unit_delete", param);
	}
	public void deleteGroupCust(Map param){
		this.getSqlMapClientTemplate().delete("GRID_GROUP_CUST_INFO.group_cust_delete", param);
	}
	public void deleteContact(Map param){
		this.getSqlMapClientTemplate().delete("GRID_CONTACT_INFO.contact_delete", param);
	}
	public void deleteNC(Map param){
		this.getSqlMapClientTemplate().delete("GRID_NEIGHBORHOOD_COMMITTEE.nc_delete", param);
	}
	public void deleteAddress(Map param){
		this.getSqlMapClientTemplate().delete("GRID_ADDRESS_INFO.address_delete", param);
	}
	/**
	 * edit community's infomation
	 * @param param
	 */
	public void modifyCommunity(Map param){
		this.getSqlMapClientTemplate().update(this.getStatementName("community_modify"), param);
	}
	/**
	 * edit community's infomation
	 * @param param
	 */
	public void modifyGroupCust(Map param){
		this.getSqlMapClientTemplate().update("GRID_GROUP_CUST_INFO.group_cust_modify", param);
	}
	/**
	 * edit community's infomation
	 * @param param
	 */
	public long addGroupCust(Map param){
		return (Long)this.getSqlMapClientTemplate().insert("GRID_GROUP_CUST_INFO.group_cust_add", param);
	}
	/**
	 * edit community's infomation
	 * @param param
	 */
	public void modifyContact(Map param){
		this.getSqlMapClientTemplate().update("GRID_CONTACT_INFO.contact_modify", param);
	}
	/**
	 * edit community's infomation
	 * @param param
	 */
	public long addContact(Map param){
		return (Long)this.getSqlMapClientTemplate().insert("GRID_CONTACT_INFO.contact_add", param);
	}
	public String getAddressDetail(String district,String neighbourhood,String street,String alley,String door_num){
		String str="";
		if(StringUtils.isNotBlank(district)){
			str+=district+" ";
		}
		if(StringUtils.isNotBlank(neighbourhood)){
			str+=neighbourhood+" ";
		}if(StringUtils.isNotBlank(street)){
			str+=street+"路";
		}if(StringUtils.isNotBlank(alley)){
			str+=alley+"弄";
		}if(StringUtils.isNotBlank(door_num)){
			str+=door_num+"号";
		}
		return str;
	}
	public Map setNamePosAndRectScale(String coodsInfo){
		Map map=new HashMap();
		//计算文字坐标
			String[] str = coodsInfo.split(",");
			int xMax = 0, yMax = 0, xMin = Integer.MAX_VALUE, yMin = Integer.MAX_VALUE;
			boolean flg = true;
			for(int i=0;i<str.length;i++){
				if(flg){
					if(xMax<Integer.parseInt(str[i])){
						xMax = Integer.parseInt(str[i]);
					}
					if(xMin>Integer.parseInt(str[i])){
						xMin = Integer.parseInt(str[i]);
					}
					flg=false;
				}else{
					if(yMax<Integer.parseInt(str[i])){
						yMax = Integer.parseInt(str[i]);
					}
					if(yMin>Integer.parseInt(str[i])){
						yMin = Integer.parseInt(str[i]);
					}
					flg=true;
				}
			String namepos = ((xMax + xMin) / 2) + "," + ((yMax + yMin) / 2);
			String rectScale = (xMax-500)+","+(yMax+800)+","+(xMin+500)+","+(yMin-800);
			map.put("namepos",namepos);
			map.put("rectScale",rectScale);
		}
		return map;
	}
}
